package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.componet.MyProgressBar;
import cn.acewill.yxd.yunxundian.model.ManagerEfficBean;
import cn.acewill.yxd.yunxundian.ui.MainActivity;

/**
 * Created by XXX on 2017/9/11.
 */

public class PersonEfficRecycleAdapter extends RecyclerView.Adapter<PersonEfficRecycleAdapter.MyViewHolder>{
    private Context context;
    private List<ManagerEfficBean> efflist;
    private lookCallBack lookCallBack;
    public PersonEfficRecycleAdapter(Context context, List<ManagerEfficBean> efflist,lookCallBack lookCallBack){
        this.context =context;
        this.efflist=efflist;
        this.lookCallBack=lookCallBack;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.person_effic_info_om,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        ManagerEfficBean bean = efflist.get(position);
        holder.binTag(bean);
        if(bean.getId()== MainActivity.id){
            holder.underNum.setVisibility(View.GONE);
        }else{
            holder.underNum.setVisibility(View.VISIBLE);
            holder.underNum.setText("下属人数（"+bean.getSubEmp()+")");
        }
        holder.managerName.setText(bean.getFirstName());
        holder.storeNum.setText(bean.getShopcount()+"");
        holder.taskRate.setText((bean.getAllNum()-bean.getUndoNum())+"/"+bean.getAllNum());
        if(bean.getUndoNum()==0&&bean.getAllNum()==0){
            holder.myProgress.setProgress(0);
        }else {
            holder.myProgress.setTag(bean);
            final ManagerEfficBean thisBean = (ManagerEfficBean) holder.myProgress.getTag();
            double a = (double) thisBean.getAllNum();
            double b = (double) thisBean.getUndoNum();
            double reProgress = (a - b) / a * (double) 100.0;
            final double progress = changeDouble(reProgress);
            final int compProgress = (int) progress;
            holder.myProgress.setProgress(compProgress);
        }

    }

    @Override
    public int getItemCount() {
        if (efflist==null){
            return 0;
        }else{
            return efflist.size();
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView taskRate;
        TextView storeNum;
        TextView managerName;
        TextView lookFor;
        MyProgressBar myProgress;
        TextView underNum;

        public MyViewHolder(View itemView) {
            super(itemView);
            lookFor= (TextView) itemView.findViewById(R.id.lookFor);
            managerName= (TextView) itemView.findViewById(R.id.managerName);
            storeNum= (TextView) itemView.findViewById(R.id.storeNum);
            taskRate= (TextView) itemView.findViewById(R.id.taskRate);
            myProgress= (MyProgressBar) itemView.findViewById(R.id.myProgress);
            underNum= (TextView) itemView.findViewById(R.id.underNum);

            lookFor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    lookCallBack.lookOnclick((ManagerEfficBean) lookFor.getTag());
                }
            });
            underNum.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    lookCallBack.underNumclick((ManagerEfficBean) underNum.getTag());
                }
            });
        }
        public void binTag(ManagerEfficBean bean){
            lookFor.setTag(bean);
            underNum.setTag(bean);
        }
    }
    public interface lookCallBack{
        void lookOnclick(ManagerEfficBean bean);
        void underNumclick(ManagerEfficBean bean);
    }
    public double changeDouble(Double dou){
        NumberFormat nf=new DecimalFormat( "0.0 ");
        dou = Double.parseDouble(nf.format(dou));
        return dou;
    }
}
